



package com.clss.commandhelper.tools.janus.util;

import android.content.Context;
import android.media.AudioManager;

/**
 * Created by HCL on 2016/5/9.
 */
public class AudioSet {
    public static AudioManager audioManager;
    public AudioSet(Context context){
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    }
    static boolean isSpeaker=false;
    static boolean isMute=false;
    static int curVolume=5;
    /**
     * 关闭扬声器
     */
    public static boolean isSpeakerphoneOn(boolean myIsSpeaker){
        isSpeaker=myIsSpeaker;
        if(isMute){
        	return false;
        }
        try {
            if (audioManager != null) {
                audioManager.setSpeakerphoneOn(isSpeaker);
                if(isSpeaker){
                    //把声音设定成Earpiece（听筒）出来，设定为正在通话中
                    audioManager.setMode(AudioManager.MODE_IN_CALL);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return isSpeaker;
    }
    /**
     * 关闭扬声器
     */
    public static void setMute(){
        isMute=!isMute;
        try {
            if (audioManager != null) {
//                audioManager.setSpeakerphoneOn(isSpeaker);
//                audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
//                    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                //判断是否为扬声器
            	if(!isMute){
            		audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, curVolume, AudioManager.STREAM_VOICE_CALL);
            		isSpeaker=isMute;
            		audioManager.setSpeakerphoneOn(audioManager.isSpeakerphoneOn());
            	}else{
            		curVolume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
            		audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 0, AudioManager.STREAM_VOICE_CALL);
            	}


                //听筒模式下设置为false
//                audioManager.setSpeakerphoneOn(false);
                //将手机的指定类型的声音调整为静音
//                audioManager.setStreamMute(audioManager.getMode(),true);
//
//                audioManager.setStreamSolo(audioManager.getMode(),false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 正常模式
     */
    public static void setModeNormal(){
        //设置回正常模式
        audioManager.setMode(AudioManager.MODE_NORMAL);

        audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);//手机铃声正常模式。

        audioManager.setMicrophoneMute(false);//*****true关闭MIC，false打开MIC
        
        isSpeaker=audioManager.isSpeakerphoneOn();
        isMute=false;
        audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, curVolume, AudioManager.STREAM_VOICE_CALL);
		audioManager.setSpeakerphoneOn(isSpeaker);
    }

    /**
     * 建立一个音频/视频或VoIP呼叫。
     */
    public static void setModeCommunication(){
        audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
    }
}
